<style lang="less">
    @import "../../../styles/table.less";
</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card title="员工管理">
                    <Row @keydown.enter.native="handleSearch">
                        <Form ref="searchForm" :model="searchForm" inline :label-width="70">
                            <Form-item label="员工姓名" prop="userName">
                                <Input type="text" v-model="searchForm.userName" placeholder="请输入员工姓名" clearable style="width: 200px"/>
                            </Form-item>
                            <Form-item label="手机号" prop="mobile">
                                <Input type="text" v-model="searchForm.mobile" placeholder="请输入手机号" clearable style="width: 200px"/>
                            </Form-item>
                            <Form-item label="部门" prop="departmentId" v-if="1==2">
                                <Select v-model="searchForm.departmentId" style="width:200px">
                                    <Option v-for="item in department" :value="item.value" :key="item.value">{{ item.label }}</Option>
                                </Select>
                            </Form-item>
                            <span v-if="drop">
                                <Form-item label="创建时间">
                                  <DatePicker v-model="selectDate" type="daterange" format="yyyy-MM-dd" clearable @on-change="selectDateRange" placeholder="选择起始时间" style="width: 200px"></DatePicker>
                                </Form-item>
                            </span>
                            <Form-item style="margin-left:-35px;" class="br">
                                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                                <Button @click="handleReset">重置</Button>
                                <a class="drop-down" @click="dropDown">{{dropDownContent}}
                                    <Icon :type="dropDownIcon"></Icon>
                                </a>
                            </Form-item>
                        </Form>
                    </Row>
                    <Row class="operation">
                        <Button @click="add" type="primary" icon="md-add">添加员工</Button>
                        <Button @click="delAll" icon="md-trash">移除员工</Button>
                        <Button @click="getDataList" icon="md-refresh">刷新</Button>
                        <circleLoading v-if="operationLoading"/>
                    </Row>
                    <Row>
                        <Alert show-icon>
                            已选择 <span class="select-count">{{selectCount}}</span> 项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" sortable="custom" @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table"></Table>
                    </Row>

                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
        <staffAdd ref="staffAdd"></staffAdd>
    </div>
</template>

<script>
    import {staffList, delStaff, getDepartment, addBlacklist} from "@/api/bus";
    import circleLoading from "../../my-components/circle-loading.vue";
    import staffDetail from "./staffDetail";
    import staffScore from "./staffScore";
    import staffAdd from "./staffAdd";

    export default {
        name: "staff-manage",
        components: {
            circleLoading,
            staffDetail,
            staffScore,
            staffAdd
        },
        data() {
            return {
                loading: false, // 表单加载状态
                operationLoading: false,
                drop: false,
                selectCount: 0, // 多选计数
                selectList: [], // 多选数据
                dropDownContent: "展开",
                dropDownIcon: "ios-arrow-down",
                department: [],
                searchForm: {
                    pageNumber: 1, // 当前页数
                    pageSize: 10, // 页面大小
                    sort: "createTime", // 默认排序字段
                    order: "desc", // 默认排序方式
                    userName: "",
                    userType: "UT_STUDENT",
                    mobile: "",
                    startDate: "",
                    endDate: "",
                    departmentId: ""
                },
                selectDate: null, //选择日期绑定modal
                columns: [
                    {
                        type: "selection",
                        width: 60,
                        align: "center"
                    },
                    {
                        type: "index",
                        width: 60,
                        align: "center"
                    },
                    {
                        title: "员工姓名",
                        key: "userName",
                        width: 130,
                        sortable: true
                    },
                    {
                        title: "员工电话",
                        key: "mobile",
                        width: 130,
                        sortable: true
                    },
                    /*{
                        title: "部门",
                        key: "deptName",
                        width: 120,
                        align: "center"
                    },*/
                    {
                        title: "服务评分",
                        key: "serveScore",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "财务评分",
                        key: "financeScore",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "维护评分",
                        key: "maintainScore",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "发展评分",
                        key: "developScore",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "营销评分",
                        key: "marketingScore",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "管理评分",
                        key: "manageScore",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "学历评分",
                        key: "educationScore",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "工作评分",
                        key: "workScore",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "能力综合评分",
                        key: "synthesisScore",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "创建时间",
                        key: "createTime",
                        sortable: true,
                        sortType: "desc",
                        width: 150
                    },
                    {
                        title: "操作",
                        key: "action",
                        width: 280,
                        align: "center",
                        fixed: "right",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            size: "small"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    "编辑"
                                ),
                                h(
                                    staffScore,
                                    {
                                        props: {
                                            id: params.row.id
                                        },
                                        style: {
                                            marginRight: "5px"
                                        }
                                    }
                                ),h(
                                    staffDetail,
                                    {
                                        props: {
                                            id: params.row.id
                                        },
                                        style: {
                                            marginRight: "5px"
                                        }
                                    }
                                ),
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "error",
                                            size: "small"
                                        },
                                        on: {
                                            click: () => {
                                                this.addBlacklist(params.row);
                                            }
                                        }
                                    },
                                    "黑名单"
                                )
                            ]);


                        }
                    }],
                data: [], // 表单数据
                total: 0 // 表单数据总数
            }
        },
        methods: {
            init() {
                this.getDataList();
                this.getDepartment();
            },
            getDepartment() {
                getDepartment(0).then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.department = res.data;
                    }
                });
            },
            getDataList() {
                // 多条件搜索用户列表
                this.loading = true;
                staffList(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.data = res.data.rows;
                        this.total = res.data.total;
                    }
                });
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getDataList();
            },
            handleReset() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.selectDate = null;
                this.searchForm.userName = "";
                this.searchForm.mobile = "";
                this.searchForm.departmentId = "";
                this.searchForm.startDate = "";
                this.searchForm.endDate = "";
                // 重新加载数据
                this.getDataList();
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            selectDateRange(v) {
                if (v) {
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getDataList();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getDataList();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getDataList();
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            setStatus(row, v) {
                let params = {
                    id: row.id,
                    status: v
                }
                setStatus(params).then(res => {
                    if (res.status === true) {
                        this.$Message.success(res.info);
                    }
                });
                ;
            },
            edit(row) {
                let query = {type: 1, id: row.id};
                this.$refs.staffAdd.showDrawer(query);
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除用户 " + v.userName + " ?",
                    onOk: () => {
                        delStaff(v.id).then(res => {
                            if (res.status === true) {
                                this.$Message.success("删除成功");
                                this.getDataList();
                            }
                        });
                    }
                });
            },
            addBlacklist(v) {
                this.$Modal.confirm({
                    title: "确认添加黑名单",
                    content: "您确认要将 " + v.userName + " 添加黑名单?",
                    onOk: () => {
                        addBlacklist(v.id).then(res => {
                            if (res.status === true) {
                                this.$Message.success("添加黑名单成功");
                                this.getDataList();
                            }
                        });
                    }
                });
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            showSelect(e) {
                this.exportData = e;
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            delAll() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择要删除的数据");
                    return;
                }
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
                    onOk: () => {
                        let ids = "";
                        this.selectList.forEach(function (e) {
                            ids += e.id + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        delStaff(ids).then(res => {
                            if (res.status === true) {
                                this.$Message.success("删除成功");
                                this.clearSelectAll();
                                this.getDataList();
                            }
                        });
                    }
                });
            },
            add() {
                let query = {type: 0}
                this.$refs.staffAdd.showDrawer(query);
            },
        },
        mounted() {
            this.init();
        }
    }
</script>
